#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
@File    :   similar_process.py    
@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2022/4/22 0022 17:49   st      1.0         None
'''
# from similar import bert_process as bp
from similar.hownet_process import get_hownet_sim
from similar.handian_process_new import get_handian_sim


def get_sim_words(words_list, threshold=0.6):
    """
    通过hownet对词组进行相似度计算，并通过阈值筛选合并
    :param words_list:
    :param threshold:
    :return:
    """

    words_len = len(words_list)
    temp_list = []
    new_words = []
    for index in range(words_len):
        if index == 0:
            temp_list.append(words_list[0])
            continue
        word_1 = words_list[index-1]
        word_2 = words_list[index]
        # 基于词典：两个词的近义词循环遍历，在相似度词典中查找是否有存在的两个近义词的相似度结果。记录相似度最大的值，作为两个词的相似度值
        # sim_bert = bp.get_bert_sim(word_1, word_2)
        sim_hownet = get_hownet_sim(word_1, word_2)
        sim_handian = get_handian_sim(word_1, word_2)
        # sim_aveg = (sim_bert + sim_hownet + sim_handian)/3
        sim_aveg = (sim_hownet + sim_handian)/2
        if sim_aveg >= threshold:
            temp_list.append(word_2)
        else:
            if len(temp_list) > 1:
                new_words.append(''.join(temp_list))
            temp_list = [word_2]
            continue
        if index == words_len - 1:
            break
    return new_words


def create_words_tuple(words_list=[]):
    """
    候选词组生成字词对计算相似度
    :param words_list:
    :return:
    """
    temp_list = []
    for i in range(len(words_list)):
        if i == len(words_list) - 1:
            break
        char1 = ''.join(words_list[:i+1])
        char2 = ''.join(words_list[i+1:])
        temp_list.append((char1, char2))
    return temp_list


def get_words_sim(words_list, voc_dict, threshold=0.6):
    """
    通过hownet对词组进行相似度计算，并通过阈值筛选合并
    :param words_list:
    :param threshold:
    :return:
    """

    words_len = len(words_list)
    if words_len < 2:
        return 1
    words_tuple = create_words_tuple(words_list)
    words_tuple_len = len(words_tuple)
    sim_bert, sim_hownet, sim_handian = 0, 0, 0
    for word_1, word_2 in words_tuple:
        # sim_bert += bp.get_bert_sim(word_1, word_2, voc_dict)
        sim_hownet += get_hownet_sim(word_1, word_2)
        sim_handian += get_handian_sim(word_1, word_2)
        # sim_count += (sim_bert + sim_hownet + sim_handian) / 3
    # sim_bert_aveg = sim_bert/words_tuple_len
    # sim_bert_aveg = get_bert_sim_avge(words_list)
    sim_hownet_aveg = sim_hownet/words_tuple_len
    sim_handian_aveg = sim_handian/words_tuple_len
    # sim_aveg = (sim_bert_aveg + sim_hownet_aveg + sim_handian_aveg)/3
    sim_aveg = (sim_hownet_aveg + sim_handian_aveg)/2
    # return sim_aveg, sim_bert_aveg, sim_hownet_aveg, sim_handian_aveg
    return sim_aveg, sim_hownet_aveg, sim_handian_aveg


if __name__ == '__main__':
    words_list = ['a', 'b']
    print(create_words_tuple(words_list))